package com.dfk.bs.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dfk.bs.common.constants.ResultCodeEnum;
import com.dfk.bs.common.exception.BsException;
import com.dfk.bs.common.vo.R;
import com.dfk.bs.entity.Eleapp;
import com.dfk.bs.entity.House;
import com.dfk.bs.entity.Housetype;
import com.dfk.bs.entity.Room;
import com.dfk.bs.service.HouseService;
import com.dfk.bs.service.HousetypeService;
import com.dfk.bs.service.RoomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 房屋类型表 前端控制器
 * </p>
 *
 * @author DDK
 * @since 2019-12-12
 */
@Api("房屋类型管理")
@CrossOrigin
@RestController
@RequestMapping("/ddkbishe/housetype")
public class HousetypeController {

    @Autowired
    private HousetypeService housetypeService;
    @Autowired
    private RoomService roomService;

    @ApiOperation("所有用房屋类型列表")
    @GetMapping("/list")
    public R list(){
        List<Housetype> list = housetypeService.list();
        return R.ok()
                .message("查询房屋类型列表成功")
                .data("items",list);
    }

    @ApiOperation("新增房屋类型")
    @PostMapping("/save")
    public R save(
            @ApiParam(name="housetype",value = "房屋类型基本信息",required = true)
            @RequestBody Housetype housetype){
        housetypeService.save(housetype);
        return R.ok()
                .message("新增房屋类型成功");
    }

    @ApiOperation("根据房屋类型id删除房屋类型")
    @DeleteMapping("/remove/{typeid}")
    public R removeById(
            @ApiParam(name = "typeid",value = "房屋类型id", required = true)
            @PathVariable String typeid){
        housetypeService.removeById(typeid);
        return R.ok()
                .message("删除房屋类型成功");
    }

    @ApiOperation("分页条件查询房屋类型列表")
    @PostMapping("/pageQuery")
    public R pageQuery(@RequestBody Map<String, Object> params){
        Integer page = (Integer) params.get("page");
        Integer limit = (Integer) params.get("limit");
        String selectname = (String) params.get("selectname");
        //业务逻辑错误，抛出自定义异常
        if(page <= 0 || limit <= 0){
            throw new BsException(ResultCodeEnum.PARAM_ERROR);
        }
        Page<Housetype> pageParam=new Page<>(page,limit);
        /*已经把查询的每一页的信息放到 pageParam中*/
        housetypeService.pageQuery(pageParam,selectname);
        List<Housetype> records = pageParam.getRecords();
        List<Room> rooms = roomService.list();
        long total = pageParam.getTotal();
        return R.ok()
                .message("分页条件查询房屋类型列表成功")
                .data("total",total)
                .data("rows",records)
                .data("rows2",rooms);
    }

    @ApiOperation(value = "根据ID修改房屋类型")
    @PutMapping("/update")
    public R updateById(
            @ApiParam(name = "housetype", value = "房屋类型对象", required = true)
            @RequestBody Housetype housetype){
        housetypeService.updateById(housetype);
        return R.ok()
                .message("修改房屋类型成功");
    }

    @ApiOperation(value = "根据name查询roomcodes")
    @PostMapping("/selectByName/{name}")
    public R selectByName(
            @ApiParam(name = "name", value = "房屋类型名称", required = true)
            @PathVariable("name") String name){
        Housetype one = housetypeService.getOne(
                Wrappers.<Housetype>lambdaQuery()
                        .eq(Housetype::getName, name), false
        );
        return R.ok().message("户型查房间").data("housetype",one);
    }



}

